var api = require('../../utils/api.js')
var app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        types:['不展示','展示'],
        type_name:'',
        type_id:'',
        name:'',
        img:'/images/icons/logo.jpg',
        imgs:[],
        suqiu:[]
    },

    onShow(){
        var that=this;

        api.post({
            url: 'user/index',
            data: {},
            success: data => {
                console.log(data) 
                that.setData({
                    user:data.data
                })
            }
        })
        //同级
            var li=[];


        api.post({
            url: 'user/getmylevel',
            data: {},
            success: data => {
                
                var cates=data.data;
                console.log(cates) 
                cates.forEach(function(item){
                    console.log(item.name);
                    li.push(item.name)

                    that.setData({
                        level_s:li,
                        levels:cates
                    })
                })
               
            }
        })





    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var id=options.id;
        this.setData({
            id:id
        })
        this.getsuqiubyid();
    },

    bindPickerChange(ret){
        console.log(ret);
        var i=ret.detail.value;
        var type_name='';
        if (i==0) {
            type_name='不展示';
        }else{
            type_name='展示';
        }
        this.setData({
            type_name:type_name
        })

    },

    /**诉求信息 */
    getsuqiubyid(){
            //getsuqiuinfobyid
        var that=this;
        var id=this.data.id;
        api.post({
            url: 'orderlist/getsuqiuinfobyid',
            data: {id:id},
            success: data => {
                console.log(data) 
                that.setData({
                    suqiu:data.data
                })
            }
        })

    },

    /**
     * 上传图片
     */
    uploadimg(){
        let that = this;
        var usrl=api.HOST;
        var imgs=this.data.imgs;

        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function (res) {
            console.log(res)
      
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFilePaths;
         
            wx.uploadFile({
              filePath: tempFilePaths[0],
              name: 'file',
              url: usrl+'/api/common/upload',
              formData:{},
              success:function(res){
                var data= JSON.parse(res.data);
                var img=data.data.fullurl;
                console.log(img)
                imgs.push(img);
                that.setData({
                    imgs:imgs
                });
        
              },
              fail: function (res) {
                console.log('上传失败');
                }
            })
          }
        })
    },

    getalldesc(ret){
        this.setData({
            name:ret.detail.value
        })
    },
    deleteimg(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;
        var imgs=this.data.imgs;
        var imgb=[];
        imgs.forEach(function(index,item){
            console.log(index,item)
            if(i!=item){
                imgb.push(index)
            }
        })

        this.setData({
            imgs:imgb
        })

    },

    /** 处理结果 */
    submits(){
        var name=this.data.name;
        var imgs=this.data.imgs;
        var type_name=this.data.type_name;
        var id=this.data.id;
        var that=this;
        api.post({
            url: 'usersave/suqiufulfillb',
            data: {'name':name,imgs:imgs,id:id},
            success: data => {
                console.log(data) 
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
                if (data.code==1) {
                    wx.switchTab({
                      url: '/pages/my/my',
                    })
                }
            }
        })


    },

    /**  提交到上级 */

    submits_fa(){
        var that=this;
        var name=this.data.name;
        var imgs=this.data.imgs;
        var id=this.data.id;

        wx.showModal({
          title: '温馨提示',
          content: '确认提交到上级部门处理',
          complete: (res) => {
    
            if (res.confirm) {
                
                api.post({
                    url: 'usersave/tosuperior',
                    data: {'name':name,id:id,imgs:imgs},
                    success: data => {
                        console.log(data) 
                        wx.showToast({
                          title: data.msg,
                          icon:'none'
                        })
                        if (data.code==1) {
                            wx.reLaunch({
                              url: '/pages/rets/index',
                            })
                        }
                    }
                })


            }
          }
        })

    },
    //转接
    submits_fb(){
        var that=this;
        var level_s=this.data.level_s;
        var levels=this.data.levels;
        var name=this.data.name;
        var id=this.data.id;

        wx.showActionSheet({
            itemList: level_s,
            success (res) {
              console.log(res);
                var index=res.tapIndex;
                var cid=levels[index].id;
              console.log(cid);
              api.post({
                url: 'usersave/suqiutosuperior',
                data: {'name':name,id:id,cid:cid},
                success: data => {
                    console.log(data) 
                    wx.showToast({
                      title: data.msg,
                      icon:'none'
                    })
                    if (data.code==1) {
                        wx.reLaunch({
                          url: '/pages/rets/index',
                        })
                    }
                }
            })



            },
            fail (res) {
              console.log(res.errMsg)
            }
          })
          
    }

})